
#===============================================================================
# ANALYSE DES SÉRIES TEMPORELLES : DONNÉES RÉELLES (YAHOO FINANCE)
#===============================================================================

# 0. CHARGEMENT DU PACKAGE POUR LE TÉLÉCHARGEMENT
# install.packages("quantmod") # Décommentez si nécessaire
library(quantmod)

# 1. RÉCUPÉRATION DES DONNÉES RÉELLES (Log-rentabilités)
symbole <- "AAPL" 

# Téléchargement des données
getSymbols(symbole, src = "yahoo", from = "2020-01-01", auto.assign = TRUE)

# Extraction du prix ajusté à la clôture
prix <- Ad(get(symbole))

# Calcul des log-rentabilités : ln(P_t) - ln(P_{t-1})
y_log_ret <- diff(log(prix))

# Suppression de la première valeur (qui est NA à cause de la fonction diff)
y <- as.numeric(na.omit(y_log_ret))
y_ts <- ts(y)

# 1.4 Visualiser la série temporelle avec Moyenne et IC 95%
mean_y <- mean(y)
sd_y <- sd(y)

# Création du graphique de base
plot(y_ts, main = paste("Log-rentabilités journalières :", symbole),
     xlab = expression(t), 
     ylab = expression(r[t]), # r pour rendement
     col = "blue", 
     type = "l", 
     lwd = 0.8)

# Ajout des lignes horizontales (Moyenne et IC 95%)
abline(h = mean_y, col = "red", lwd = 2)
abline(h = mean_y + 1.96 * sd_y, col = "darkred", lty = 2, lwd = 1.5)
abline(h = mean_y - 1.96 * sd_y, col = "darkred", lty = 2, lwd = 1.5)

# Légende
legend("topright", 
       legend = c("Rentabilités", "Moyenne", "IC 95% (Moyenne ± 1.96*SD)"),
       col = c("blue", "red", "darkred"), 
       lty = c(1, 1, 2), 
       lwd = c(0.8, 2, 1.5), 
       cex = 0.7,
       bg = "white")

# 2. ANALYSE EXPLORATOIRE DES DONNÉES

# 2.1 Statistiques descriptives
print(summary(y))

# Calcul de la variance
n <- length(y)
y_centered <- y - mean_y
variance_y <- sum(y_centered^2) / n
print(variance_y)

# 2.1.1 Fonctions d'autocovariance et d'autocorrélation
lag_max <- 10
acvf <- numeric(lag_max + 1)
acf_vals <- numeric(lag_max + 1)
lags <- 0:lag_max

# Lag k = 0
acvf[1] <- variance_y
acf_vals[1] <- 1.0

# Calcul pour les retards k de 1 à lag_max
for (k in 1:lag_max) {
  gamma_k <- sum(y_centered[1:(n - k)] * y_centered[(k + 1):n]) / n
  acvf[k + 1] <- gamma_k
  acf_vals[k + 1] <- gamma_k / variance_y
}

# Affichage des premières valeurs
cat("\n--- Fonction d'autocovariance (ACVF) et d'autocorrélation (ACF) ---\n")
cat("Retard |   ACVF    |    ACF   \n")
for (k in 0:min(10, lag_max)) {
  cat(sprintf("  %2d   | %8.6f | %8.4f\n", k, acvf[k+1], acf_vals[k+1]))
}

# 2.2 Distribution : Histogramme + Densité
dens <- density(y)
ymax <- max(hist(y, plot = FALSE)$density, max(density(y)$y)) * 1.15

hist(y, prob = TRUE, main = paste("Distribution des rentabilités :", symbole), 
     xlab = "Log-rentabilité", 
     col = "lightblue", 
     border = "black",
     ylim = c(0, ymax))
lines(density(y), col = "red", lwd = 2)

# 2.3 Boxplot 
boxplot(y, main = paste("Boxplot des rentabilités :", symbole), ylab = "Log-rentabilité", col = "blue", horizontal = TRUE)

# 2.4 Autocorrelogramme
ci_95 <- 1.96 / sqrt(n)

plot(lags, acf_vals, type = "h", lwd = 2, col = "blue",
     main = "Autocorrelogramme des rentabilités",
     xlab = expression(k), 
     ylab = expression(gamma[k]),
     ylim = c(min(-1, min(acf_vals) - 0.1), max(1, max(acf_vals) + 0.1)))

abline(h = 0, col = "black", lwd = 1)
abline(h = ci_95, col = "red", lty = 2, lwd = 1.5)
abline(h = -ci_95, col = "red", lty = 2, lwd = 1.5)

legend("topright", 
       legend = c("Valeurs ACF", "IC 95% (Bruit blanc)"),
       col = c("blue", "red"), 
       lty = c(1, 2), 
       lwd = c(2, 1.5), 
       bg = "white",
       cex = 0.8)

